function ws_send(data,ismaser=true) {
    var defaultdata = {
        'roomid':roomId,
        'toid':0,
        'reqtime':new Date().getTime()
    }
    const result = Object.assign(defaultdata,data);
    if(ismaser){
        if(content.master == 1){
            ws.send(JSON.stringify(result));
        }
    }else{
        ws.send(JSON.stringify(result));
    }
}
/******* 视屏控件 */
var controls = {
    setting: type == 'single' ? true : 'none',
    synchronization: type != 'single',
    fullweb:'none',
    next: true,
    play:  type != 'single' ? 'Prohibit' : '',
    wrap:  type != 'single' ? 'Prohibit' : '',
}
var dplayer = new DPlayer({
    container: document.getElementById("myVideo"),
    controls: controls,
    video: {
        // url: 'https://youku.com-ok-56.com/20191203/20645_328726a2/index.m3u8'
    },
    volume:0,
    contextmenu: []
});
dplayer.volume(0)
/** 切换视频 */
function playNext() {
    if(content.master==1){
        content.replace(0);
    }else{
        dplayer.notice('房主才可操作')
    }
}
if(type != 'single'){
    /** 进度同步 */
    function synchronization() {
        // 房主发起同步
        if(content.master==1){
            let playing = content.playing;
            let currentTime = dplayer.video.currentTime+''
            ws_send({type:'synchro_seeked_group',currentTime:currentTime,playing:playing})
        }else{  //成员发起同步
            ws_send({type:'synchro_seeked_req'},false)
        }
    }
    var canplayinfo = false
    /** 可流畅播放 */
    dplayer.on('canplaythrough', function() {
        if(canplayinfo !== false){
            var cha = 1,seeked;
            if(canplayinfo.playstatus=='play'){
                cha += Math.floor((new Date().getTime() - canplayinfo.reqtime)/1000);
                seeked = cha + Math.floor(canplayinfo.currentTime)
            }else{
                seeked = canplayinfo.currentTime;
            }
            if(Math.abs(seeked - dplayer.video.currentTime)>3){
                dplayer.seek(seeked);
            }else{
                eval('dplayer.'+canplayinfo.playstatus+"()")
                canplayinfo = false
            }
        }
    });
    /** 播放 */
    dplayer.on('play', function() {
        console.log('play')
        content.playstatus = 'play'
        if(content.master==1){
            let currentTime = dplayer.video.currentTime+''
            ws_send({type:'play',currentTime:currentTime})
        }
    });
    /** 暂停 */
    dplayer.on('pause', function() {
        console.log('pause')
        content.playstatus = 'pause'
        if(content.master==1){
            let currentTime = dplayer.video.currentTime+''
            ws_send({type:'pause',currentTime:currentTime})
        }
    });
    /** 进度控制 */
    dplayer.on('seeked', function() {
        if(content.master==1){
            let currentTime = dplayer.video.currentTime+''
            ws_send({type:'seeked',currentTime:currentTime,playstatus:content.playstatus})
        }
    });
}



/** *****************  end Play **********     */

/**  弹窗  */
var shownum = 0
var autojoin = false
if(type == 'online'){
    shownum = 'createRoom'
}else if( type=='join' && roomStatus==2){
    shownum = 'password'
}else if( type=='join' && roomStatus==1){
    autojoin = true;
}else if(type=='join'){
    autojoin = true;
}
if(type !='single'){
    loading = layer.open({
        type: 2,
        content: '连接中，请等待',
        shadeClose: false,
    });
}
var showtap = new Vue({
    el: '#tap',
    data: {
        tap: shownum,
        submit: {
            status: 1,
            cansex: 0,
            canpeople: 2,
            password:''
        },
    },
    methods: {
        creatroom: function () {
            var that = this
            var data = Object.assign({},that.submit);
            data.client_id =  WebSocket_data.client_id
            mpostend('/index.php/index/room/create', data, function (res) {
                that.tap = 0
                history.replaceState(history.state, '', '/index.php/index/room/join/' + res.data.room)
                roomId = res.data.room;
                roomStatus = that.submit.status
                content.master = room.master = 1
                document.cookie = "room_token="+res.data.room_token;
                dplayer.setControls({
                    next: true,
                    play: '',
                    wrap: '',
                })
            });
        },
        goback: function () {
            history.back()
        },
        joinroom: function () {
            var that = this
            mpostend('/index.php/index/room/join/'+roomId, {password:that.submit.password,client_id:WebSocket_data.client_id}, function (res) {

            });
        }
    }
})

var anthology = new Vue({
    el:'#anthology',
    data:{
        show:0,
        info:[]
    },
    methods:{
        anthology:function (e) {
            var that = content
            var type = "flag="+anthology.info[e].flag+"&id="+anthology.info[e].id
            if (that.submit.url == '') {
                mylayer.msg('请输入解析地址')
                return false
            }
            mpostend('/index.php/index/video/' + that.submit.api, {url: type,type:1}, function (res) {
                var data={}
                try {
                    data = eval(that.submit.api + "('" + res.data.curl + "','"+type+"')")
                }catch (r){
                    layer.close(loading)
                    mylayer.msg('解析错误，请扫后再试或切换路线')
                    return false
                }
                layer.close(loading)
                if(data.info == undefined || data.info.length==0){
                    mylayer.msg('未找到资源')
                    return false
                }
                if( data.chack == undefined || data.chack !== 0){
                    return false;
                }
                that.yuan = data;
                anthology.show=0
            }, function () {
                anthology.show=0
            }, {status: 3, msg: '解析中，请耐心等待'});
        },
        closeshow:function () {
            this.show=0
        },
    }
})
/********  页面控制  ********/
/** 切换页面 */
var room = new Vue({
    el: '#index',
    data: {
        show: 1,    //显示栏目
        master: type == 'single' ? 1 : 0,  //是否房主
    },
    methods: {
        /** 修改show */
        setshow: function (show) {
            this.show = show;
            content.show = show
            if (show == 1) {
                $("#play").show()
            } else {
                $("#play").hide()
            }
        },
    }
})
/** 切换内容 */
var content = new Vue({
    el: '#list',
    data: {
        show: 1,    //显示栏目
        menu:1, //切换
        master: type == 'single' ? 1 : 0, //是否房主
        playlist: [
            {name:'第926集 海贼王',url:'http://tudou.diediao-kuyun.com/20200328/13862_fa1c379d/index.m3u8'},
            {name:'第925集 海贼王',url:'http://tudou.diediao-kuyun.com/20200322/13468_7f8768b5/index.m3u8'},
            {name:'第924集 海贼王',url:'http://tudou.diediao-kuyun.com/20200316/13160_07d4c8c5/index.m3u8'},
        ],    //播放列表
        playing: {},
        playstatus:'pause',
        submit: {    //解析表单
            url: '',
            api: selectedapi
        },
        search:{    //搜索表单
            name: '',
            api: search_default
        },
        searchapi:'',//最后搜索api
        yuan: { //解析结果
            chack: 0,   //选择源
            title: '',
            // info: [ //源列表
            //
            // ]
        },
        chackedmove:0,//当前选择的视频
        searchinfo:[//搜索结果

        ]
    },
    methods: {
        /** 本站搜索 */
        sendsearch: function () {
            var that = this
            if (that.search.name == '') {
                mylayer.msg('请输入视频名称')
                return false
            }
            mpostend('/index.php/index/search', that.search, function (res) {
                if(res.data.movelist.length<=0){
                    mylayer.msg('未找到相关视频')
                    layer.close(loading)
                }
                for (let i in res.data.movelist){
                    res.data.movelist[i].child = [];
                }
                that.searchinfo=res.data.movelist;
                let o = Object.assign({},that.search);
                that.searchapi = o.api
                layer.close(loading)
            }, '', {status: 3, msg: '搜索中，请耐心等待'});
        },
        /** 获取 */
        getdd:function (index) {
            var that = this
            this.chackedmove = that.searchinfo[index].id
            // if(!that.searchinfo[index].hasOwnProperty('child')){
            if(that.searchinfo[index].child.length<=0){
                mpostend('/index.php/index/movieinfo', {api:that.searchapi,id:that.chackedmove}, function (res) {
                    var list = [];
                    for (let i in  res.data.moveinfo.url){
                        list[i] = {}
                        let a = res.data.moveinfo.url[i].split("$");
                        list[i].name = a[0]
                        list[i].url = a[1]
                    }
                    that.searchinfo[index].child = list
                }, '', {status: 1, msg: '搜索中，请耐心等待'});
            }

        },
        /** 发起解析 */
        analysis: function () {
            var that = this
            if (that.submit.url == '') {
                mylayer.msg('请输入解析地址')
                return false
            }
            mpostend('/index.php/index/video/' + that.submit.api, {url: that.submit.url}, function (res) {
                var data={}
                try {
                    data = eval(that.submit.api + "('" + res.data.curl + "','"+that.submit.url+"')")
                }catch (r){
                    layer.close(loading)
                    mylayer.msg('解析错误，请扫后再试或切换路线')
                    return false
                }
                layer.close(loading)
                if(data.info == undefined || data.info.length==0){
                    mylayer.msg('未找到资源')
                    return false
                }
                if( data.chack == undefined || data.chack !== 0){
                    return false;
                }
                that.yuan = data;
            }, '', {status: 3, msg: '解析中，请耐心等待'});
        },
        /** 添加播放列表 */
        addPlay: function (e, pname) {
            var data = Object.assign({}, e)
            data.name = data.name + ' ' + pname
            var playlist = this.playlist;
            if (playlist.length >= 30) {
                mylayer.msg('播放列表最多30个')
                return false
            }
            playlist.push(data);
            mylayer.msg('添加成功')
        },
        /** 全部添加 */
        addAll: function (moveinfo) {
            loading = layer.open({
                type: 2,
                content: '添加中',
                shadeClose: false,
            });
            var playlist = this.playlist;
            if ((playlist.length + moveinfo.child.length)>30) {
                layer.close(loading)
                mylayer.msg('播放列表最多30个')
                return false
            }
            for (let i in moveinfo.child){
                var data = {}
                data.name = moveinfo.child[i].name + moveinfo.name
                data.url = moveinfo.child[i].url
                playlist.push(data);
            }

            layer.close(loading)
            mylayer.msg('添加成功')
        },
        /** 清除播放列表 */
        clearPlay: function (index) {
            var playlist = this.playlist;
            if (index == undefined) {
                console.log('清空播放列表')
                playlist.splice(0, playlist.length)
                return false
            }
            playlist.splice(index, 1)
            console.log('清理播放列表', index)
        },
        /** 切换视频 */
        replace: function (index=0) {
            var playlist = content.playlist;
            if(playlist.length<=1){
                dplayer.notice('请先添加视频')
                return false
            }
            var playing = playlist[index]
            content.playing = playing;
            dplayer.switchVideo({
                url: playing.url ? playing.url : ''
            })
            playlist.splice(index, 1)
            if(type != 'single'){
                synchronization()
            }
            mylayer.msg('切换视屏')
        },
        /** 置顶 */
        up: function (e) {
            var playlist = this.playlist;
            var newplaylist = [];
            if (e >= 1 && playlist.length > 2) {
                newplaylist[0] = playlist[e]
                for (let i = 0; i < playlist.length; i++) {
                    if (e != i) {
                        newplaylist.push(playlist[i]);
                    }
                }
                this.playlist = newplaylist
            }
        },
        /** 新窗口打开 */
        newWindows:function (url) {
            var newWin = window.open();
            newWin.location.href = url
        }
    }
})

/** 接入时 */

/**           解析路线处理           */
function ledboke(res,url) {
    var start = res.indexOf('{');
    var end = res.lastIndexOf('}') + 1;
    var json = res.slice(start, end)
    var data = JSON.parse(json);
    for (let i in data.info) {
        var yuan = data.info[i];
        for (let j in yuan.video) {
            let arr = yuan.video[j].split("$")
            yuan.video[j] = {
                name: arr[0],
                url: arr[1]
            }
        }
    }
    data.chack = 0;
    return data;
}
function wandhi(res,url) {
    var start = res.indexOf('{');
    var end = res.lastIndexOf('}') + 1;
    var json = res.slice(start, end)
    var data = JSON.parse(json);
    if(data.success != 1){
        return data;
    }
    if (url.indexOf('http') != -1 || (url.indexOf('flag') != -1 && url.indexOf('id') != -1)){
        for (let i in data.info) {
            var yuan = data.info[i];
            for (let j in yuan.video) {
                let arr = yuan.video[j].split("$")
                yuan.video[j] = {
                    name: arr[0],
                    url: arr[1]
                }
            }
        }
        data.chack = 0;
    }else{
        for (let i in data.info){
            data.info[i].title = decodeURI(data.info[i].title)
        }
        anthology.show=1
        anthology.info = data.info
        layer.close(loading)
    }
    return data;
}
function dhplay(res,url) {
    var start = res.indexOf('{');
    var end = res.lastIndexOf('}') + 1;
    var json = res.slice(start, end)
    var data = JSON.parse(json);
    if (url.indexOf('http') != -1 || (url.indexOf('flag') != -1 && url.indexOf('id') != -1)){
        data.chack = 0;
    }else{
        for (let i in data.info){
            data.info[i].title = decodeURI(data.info[i].title)
        }
        anthology.show=1
        anthology.info = data.info
        layer.close(loading)
    }
    return data
}